package com.cloudkinto.controller.user;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.vo.role.menu.MenuNewTreeRes;
import com.cloudkinto.service.user.UserService;
import com.cloudkinto.session.UserSessionManager;
import com.cloudkinto.vo.user.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-05-27
 */
@RestController
@Api(tags = "OMS用户模块")
@RequestMapping("/api/admin/oms/user")
public class UserOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(UserOmsController.class);

    @Autowired
    private UserService service;
    @Autowired
    private UserSessionManager userSessionManager;

    @PostMapping("login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public SingleResult login(@RequestBody @Validated UserLoginReq req, HttpServletRequest request) {
        return SingleResult.success("登录成功", service.login(req, request, "oms"));
    }

    @UserSessionRequired
    @GetMapping("getMenuTree")
    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = MenuNewTreeRes.class))
    public SingleResult getMenuTree(@RequestHeader String token) {
        return SingleResult.success("", service.getMenuTree(token, "oms"));
    }


    @UserSessionRequired
    @GetMapping("getUserInfo")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = UserInfoRes.class))
    public SingleResult getUserInfo(@RequestHeader String token) {
        return SingleResult.success("", service.getUserInfo(token, "oms"));
    }


    @UserSessionRequired
    @PostMapping("main")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public SingleResult main(HttpServletRequest request) {
        return SingleResult.success("登录成功", service.main(request.getHeader("token"), "oms"));
    }


    @UserSessionRequired
    @PostMapping("logout")
    @ApiOperation(value = "用户退出登录", notes = "用户退出登录")
    public SingleResult logout(@RequestHeader String token) {
        service.logout(token);
        return SingleResult.success("退出成功", null);
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "初始化", notes = "初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId(), getTenantId(), "oms");
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated UserBuildReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId, companyId, getTenantId(), "oms"));
    }


    @UserSessionRequired
    @PostMapping("check")
    @ApiOperation(value = "用户审核/禁用", notes = "用户审核/禁用")
    public SingleResult check(@RequestBody @Validated UserCheckReq req) {
        Long userId = getCurrentUserId();
        service.check(req, userId,  getCompanyId(), getTenantId(), "oms");
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改个人信息", notes = "修改")
    public SingleResult update(@RequestBody @Validated UserUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getCompanyId(), getTenantId(), "oms"));
    }

    @UserSessionRequired
    @PutMapping("updatePassword")
    @ApiOperation(value = "修改密码", notes = "修改")
    public SingleResult updatePassword(@RequestBody @Validated UpdatePasswordReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updatePassword(req, userId, getCompanyId(), getTenantId(), "oms"));
    }


    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "userStatus", value = "状态"),
            @ApiImplicitParam(name = "roleId", value = "角色"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = UserListPageRes.class))
    public PageResult listPage(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String roleId = request.getParameter("roleId");
        String userStatus = request.getParameter("userStatus");
        map.put("roleId", roleId);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("companyId", getCompanyId());
        map.put("source", "oms");
        map.put("userType", 2);
        map.put("userStatus", userStatus);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId, getCompanyId(), getTenantId(), "oms"));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "删除账号", notes = "删除账号")
    public SingleResult detail(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("查询成功", service.detail(id,  getCompanyId(), getTenantId(), "oms"));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id,  getCompanyId(), getTenantId(), "oms");
    }


    @GetMapping("verificationCode")
    @ApiOperation(value = "获取验证码", notes = "")
    public SingleResult verificationCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return service.verificationCode();
    }


}

